package com.wei.czz.framework.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.common.dao.SmsTemplateFlowDao;
import com.wei.czz.framework.common.entity.SmsTemplateFlowEntity;
import com.wei.czz.framework.common.service.SmsTemplateFlowService;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-06-17 21:41:59
 * className: SmsTemplateFlowServiceImpl
 * version: 1.0
 * description:
 */
@Service("smsTemplateFlowService")
public class SmsTemplateFlowServiceImpl extends ServiceImpl<SmsTemplateFlowDao, SmsTemplateFlowEntity> implements SmsTemplateFlowService {

    private static final Logger log = LoggerFactory.getLogger(SmsTemplateFlowServiceImpl.class);

    @Override
    public void saveOrEdit(SmsTemplateFlowEntity smsTemplateFlow) {
        Long id = smsTemplateFlow.getId();
        if (Objects.isNull(id)) {
            int count = baseMapper.insert(smsTemplateFlow);
            log.info("插入短信模板流水完成。count={}", count);
        } else {
            int count = baseMapper.updateById(smsTemplateFlow);
            log.info("修改短信模板流水完成。count={}", count);
        }
    }

    @Override
    public SmsTemplateFlowEntity find(Long masterId, Integer version) {

        LambdaQueryWrapper<SmsTemplateFlowEntity> smsTemplateFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        smsTemplateFlowLambdaQueryWrapper.eq(SmsTemplateFlowEntity::getMasterId, masterId)
                .eq(SmsTemplateFlowEntity::getVersion, version)
                .eq(SmsTemplateFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<SmsTemplateFlowEntity> smsTemplateFlowList = baseMapper.selectList(smsTemplateFlowLambdaQueryWrapper);
        log.info("查询短信模板流水完成。masterId={} version={} size={}", masterId, version, smsTemplateFlowList.size());
        return smsTemplateFlowList.get(0);
    }

    @Override
    public List<SmsTemplateFlowEntity> findList(Long masterId, Integer type, Integer channel) {
        if (ObjectUtils.allNull(masterId, type, channel)) {
            log.info("获取短信模板流水，传递的主表主键、短信类型和短信渠道均为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<SmsTemplateFlowEntity> smsTemplateFlowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        smsTemplateFlowLambdaQueryWrapper.eq(Objects.nonNull(masterId), SmsTemplateFlowEntity::getMasterId, masterId)
                .eq(Objects.nonNull(type), SmsTemplateFlowEntity::getType, type)
                .eq(Objects.nonNull(channel), SmsTemplateFlowEntity::getChannel, channel)
                .eq(SmsTemplateFlowEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<SmsTemplateFlowEntity> smsTemplateFlowList = baseMapper.selectList(smsTemplateFlowLambdaQueryWrapper);
        log.info("查询短信模板流水完成。masterId={} type={} channel={} list.size={}", masterId, type, channel,
                smsTemplateFlowList.size());
        return smsTemplateFlowList;
    }

}